<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="内容名称" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入内容名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属栏目" prop="categoryId">
        <el-cascader
          v-model="queryParams.categoryId"
          :options="categoryTree"
          :props="{ checkStrictly: true, expandTrigger: 'hover', value: 'id', label: 'title', emitPath: false }"
          clearable
        ></el-cascader>
      </el-form-item>
      <el-form-item label="归属" prop="belong">
        <el-select v-model="queryParams.belong" placeholder="请选择归属" clearable size="small" @change="queryBelongChange">
          <el-option
            v-for="dict in belongOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="所属部门" prop="depart">
        <el-select v-model="queryParams.departId" placeholder="请选择所属部门" clearable size="small">
          <el-option
            v-for="dict in departOptions"
            :key="dict.id"
            :label="dict.name"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择类型" clearable size="small">
          <el-option
            v-for="dict in typeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option key="0" label="审核未通过" value="0"/>
          <el-option key="1" label="审核通过" value="1"/>
          <el-option key="2" label="等待审核" value="2"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="contentList">
      <el-table-column label="标题" align="center" prop="title"/>
      <el-table-column label="所属部门" align="center" prop="depart" :formatter="departFormat"/>
      <el-table-column label="发布者" align="center" prop="createBy"/>
      <el-table-column label="所属栏目" align="center" prop="categoryName"/>
      <el-table-column label="类型" align="center" prop="type" :formatter="typeFormat"/>
      <el-table-column label="状态" align="center" prop="status" :formatter="statusFormat"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status == 1"
            active-text="通过"
            inactive-text="不通过"
            @change="auditStatus($event, scope.row.id)"
          >
          </el-switch>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import {
  getContent,
  delContent,
  addContent,
  updateContent,
  exportContent,
  getSourceTree, listAuditContent
} from '@/api/business/content'
import { getCategoryTree } from '@/api/business/category'
import { getDepartByBelong } from '@/api/business/depart'

export default {
  name: 'audit',
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // Content表格数据
      contentList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 类型 1text 2pdf 3video字典
      typeOptions: [],
      // 状态 0禁用 1启用字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        categoryId: null,
        type: null,
        status: null,
        belong: null,
        departId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      // 栏目树
      categoryTree: [],
      // 部门树
      sourceTree: [],
      belongOptions: [],
      departOptions: []
    }
  },
  created() {
    this.getCategoryTree()
    this.getSourceTree()
    this.getList()
    this.getDicts('business_content_type').then(response => {
      this.typeOptions = response.data
    })
    this.getDicts('business_belong').then(response => {
      this.belongOptions = response.data
    })
  },
  methods: {
    queryBelongChange(newValue) {
      if (newValue != '' && newValue != null && newValue != undefined) {
        this.getDepartByBelong(newValue)
      }
    },
    getDepartByBelong(belong) {
      getDepartByBelong(belong).then(response => {
        this.departOptions = response.data
      })
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        title: null,
        categoryId: null,
        type: null,
        status: null,
        belong: null,
        departId: null
      }
      this.handleQuery()
    },
    auditStatus(value, contentId) {
      let data = {
        id: contentId,
        status: value ? 1 : 0
      }
      updateContent(data).then(response => {
        this.msgSuccess('审批成功')
        this.open = false
        this.getList()
      })
    },
    /** 查询部门树 */
    getSourceTree() {
      getSourceTree().then(response => {
        this.sourceTree = response.data
      })
    },
    /** 查询栏目树 */
    getCategoryTree() {
      getCategoryTree().then(response => {
        this.categoryTree = response.data
      })
    },
    /** 查询Content列表 */
    getList() {
      this.loading = true
      listAuditContent(this.queryParams).then(response => {
        this.contentList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 类型 1text 2pdf 3video字典翻译
    typeFormat(row, column) {
      return this.selectDictLabel(this.typeOptions, row.type)
    },
    // 状态 0禁用 1启用字典翻译
    statusFormat(row, column) {
      if (row.status == 0) {
        return '审核未通过'
      } else if (row.status == 1) {
        return '审核通过'
      } else if (row.status == 2) {
        return '等待审核'
      }
    },
    departFormat(row, column) {
      return row.belongName + ' --- ' + row.departName
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    }
  }
}
</script>
